package log

import (
	"fmt"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var Log1 *MicroLogs

type MicroLogs struct {
	log *zap.Logger
}

//format暂时无用
func InitZapLogs(level, format string) {
	var err error
	if Log == nil {
		Log1 = new(MicroLogs)
		encoderConfig := zapcore.EncoderConfig{
			TimeKey:        "time",
			LevelKey:       "level",
			NameKey:        "logger",
			CallerKey:      "caller",
			MessageKey:     "msg",
			StacktraceKey:  "stacktrace",
			LineEnding:     zapcore.DefaultLineEnding,
			EncodeLevel:    zapcore.LowercaseLevelEncoder, // 小写编码器
			EncodeTime:     zapcore.ISO8601TimeEncoder,    // ISO8601 UTC 时间格式
			EncodeDuration: zapcore.SecondsDurationEncoder,
			EncodeCaller:   zapcore.FullCallerEncoder, // 全路径编码器
		}

		// 设置日志级别
		atom := zap.NewAtomicLevelAt(zap.DebugLevel)

		config := zap.Config{
			Level:            atom,                                                              // 日志级别
			Development:      true,                                                              // 开发模式，堆栈跟踪
			Encoding:         "console",                                                         // 输出格式 console 或 json
			EncoderConfig:    encoderConfig,                                                     // 编码器配置
			InitialFields:    map[string]interface{}{"service": viper.GetString("server.type")}, // 初始化字段，如：添加一个服务器名称
			OutputPaths:      []string{"stdout"},                                                // 输出到指定文件 stdout（标准输出，正常颜色） stderr（错误输出，红色）
			ErrorOutputPaths: []string{"stderr"},
		}

		// 构建日志
		Log1.log, err = config.Build()
		if err != nil {
			panic(fmt.Sprintf("log 初始化失败: %v", err))
		}
		Log1.log.Info("log 初始化成功")
	}
}

func (l *MicroLogs) Info(args ...interface{}) {
	l.log.Info(fmt.Sprintln(args))
}
func (l *MicroLogs) Infof(format string, args ...interface{}) {
	l.log.Info(fmt.Sprintf(format, args...))
}

func (l *MicroLogs) Debug(args ...interface{}) {
	l.log.Debug(fmt.Sprintln(args))
}
func (l *MicroLogs) Debugf(format string, args ...interface{}) {
	l.log.Debug(fmt.Sprintf(format, args...))
}

func (l *MicroLogs) Error(args ...interface{}) {
	l.log.Error(fmt.Sprintln(args))
}
func (l *MicroLogs) Errorf(format string, args ...interface{}) {
	l.log.Error(fmt.Sprintf(format, args...))
}

func (l *MicroLogs) Warning(args ...interface{}) {
	l.log.Warn(fmt.Sprintln(args))
}
func (l *MicroLogs) Warningf(format string, args ...interface{}) {
	l.log.Warn(fmt.Sprintf(format, args...))
}
